from langchain.chains.llm import LLMChain
from langchain_core.prompts import PromptTemplate

from ChatGLM_new import xinghuo_llm
from langchain.output_parsers import CommaSeparatedListOutputParser, DatetimeOutputParser, ResponseSchema, \
    StructuredOutputParser

print("##################逗号分隔列表输出解析器CommaSeparatedListOutputParser########################")
output_parser = CommaSeparatedListOutputParser()
format_instructions = output_parser.get_format_instructions()
prompt = PromptTemplate(
    template="请列出五个 {subject}.\n{format_instructions}",
    input_variables=["subject"],
    partial_variables={"format_instructions": format_instructions}
)
print(prompt)
input = prompt.format(subject="中国的名山")
#output = xinghuo_llm.invoke(input)
#res=output_parser.parse(output.content)
#print(res)

print("################日期时间解析器DatetimeOutputParser##########################")



output_parser = DatetimeOutputParser()
template = """Answer the users question:

{question}

{format_instructions}"""
prompt = PromptTemplate.from_template(template, partial_variables={"format_instructions": output_parser.get_format_instructions()})
print(prompt)
chain = LLMChain(prompt=prompt, llm=xinghuo_llm)
#output = chain.invoke("中国建国是哪一年?")
#print(output)

print("#########结构化输出解析器##########")

response_schemas = [
    ResponseSchema(name="name", description="学生的姓名"),
    ResponseSchema(name="age", description="学生的年龄")
]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)

format_instructions = output_parser.get_format_instructions()
prompt = PromptTemplate(
    template="回答下面问题,注意不要显示键的描述信息.\n{format_instructions}\n{question}",
    input_variables=["question"],
    partial_variables={"format_instructions": format_instructions}
)

_input = prompt.format_prompt(question="给我一个女孩的名字?")
print(_input)
output = xinghuo_llm.invoke(_input.to_string())
print(output)
print(output_parser.parse(output.content))

